package com.j48.entrancegurad.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.j48.entrancegurad.model.Host;
import com.j48.entrancegurad.model.User;
import com.j48.entrancegurad.model.Visitor;
import com.j48.entrancegurad.service.IVisitorService;
import com.j48.entrancegurad.tools.HttpRequest;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.j48.entrancegurad.service.IHostService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.j48.entrancegurad.service.IUserService;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/***
 * 传递数据到前端
 */
@Controller
public class WxRegisterController {
    @Autowired
    private IHostService hostservice;
    @Autowired
    private IUserService userService;
    @Autowired
    private IVisitorService visitorService;

    @RequestMapping(value = "/Wxregister",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String host(Host hosts,HttpServletRequest request){

        ArrayList<Host> arrayList=new ArrayList<Host>();
        arrayList=hostservice.selectAll();
        return JSONArray.toJSONString(arrayList);
    }
    /***
     * 获取注册时用户的信息存放到session。
     */
    @RequestMapping(value = "/Wxsave",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String WxSave(Host host,HttpServletRequest request,User user){
        Host host2=new Host();
        //获取前端数据
        String host_name=request.getParameter("host_name");
        String host_phone=request.getParameter("host_phone");
        String host1=request.getParameter("host");
        String user_name=request.getParameter("accounts");
        String host_housenumber=request.getParameter("host_housenumber");
        String host_building=request.getParameter("rst");
        String host_unit=request.getParameter("test");
        int host_building2=Integer.parseInt(host_building);
        int host_unit2=Integer.parseInt(host_unit);
        int host_housenumber2=Integer.parseInt(host_housenumber);
        ArrayList<String> Alllist=new ArrayList<String>();
        Alllist.add(host_name);
        Alllist.add(host1);
        Alllist.add(host_housenumber);
        Alllist.add(host_phone);
        Alllist.add(host_building);
        Alllist.add(host_unit);
        Alllist.add(user_name);
        request.getSession().setAttribute("Wxlist",Alllist);
        Timestamp createTime=new Timestamp(new Date().getTime());
        long host_phone1=Long.parseLong(host_phone);
        host.setHost_phone(host_phone1);
        host2=hostservice.selectByPhone(host);
        if(host2==null){
            return "false";
        }else{
            int host_building1=host2.getHost_building();
            int host_unit1=host2.getHost_unit();
            int host_number1=host2.getHost_housenumber();
            String hostName=host2.getHost_name();
            if(host_building1==host_building2&&host_unit1==host_unit2&&host_number1==host_housenumber2&&hostName.equals(host_name)){
                int host_id=host2.getHost_id();
                String openId=(String)request.getSession().getAttribute("openId");
                user.setUser_name(user_name);
                user.setUser_host_id(host_id);
                user.setUser_createtime(createTime);
                user.setUser_open_id(openId);
                userService.insertUser(user);
            }else{
                return "false";
            }
            return "true";
        }
    }

    /***
     * 获取注册时用户的信息存放到session。
     */
    @RequestMapping(value = "/WxAdds",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String WxAdds(HttpServletRequest request){
        HttpSession session=request.getSession();
        String sessionId=session.getId();
       String openId=(String)request.getSession().getAttribute("openId");
       User user =new User();
       Host host=new Host();
       user.setUser_open_id(openId);
       User user1=userService.selectByToken(user);
      String user_Name=user1.getUser_name();

       int host_id=user1.getUser_host_id();
       host.setHost_id(host_id);
       Host host1=hostservice.selectById(host);
       String host_housenumber=host1.getHost_housenumber()+"";
       String host_building=host1.getHost_building()+"";
        String host_unit=host1.getHost_unit()+"";
        ArrayList<String> Wxlist=new ArrayList<String>();
        Wxlist.add(user_Name);
        Wxlist.add(host_housenumber);
        Wxlist.add(host_building);
        Wxlist.add(host_unit);

        return  JSONArray.toJSONString(Wxlist);
    }
    /***
     * 将小程序页面上添加访客的信息传到后台并保存到数据库
     */
    @RequestMapping(value = "/WxAddVisitor",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String WxAddVisitor(HttpServletRequest request, User user, Visitor visitor){
        ArrayList<User> userlist=new ArrayList<User>();
        Timestamp createTime=new Timestamp(new Date().getTime());
        String visitorName=request.getParameter("visitor_name");
        int visitorNumber=Integer.parseInt(request.getParameter("visitor_number"));
        String userName=request.getParameter("user");
        String openId=(String)request.getSession().getAttribute("openId");
        String year=(String)request.getParameter("year");
        String hour=(String)request.getParameter("time");
        user.setUser_name(userName);
        user.setUser_open_id(openId);
        userlist=userService.selectByNameAndOpen(user);
        int userId=userlist.get(0).getUser_id();
        String comeTime=year+" "+hour+":00";
        long ss=0;
        try {
            ss=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(comeTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Timestamp comeTime1=new Timestamp(ss);
        visitor.setVisitor_name(visitorName);
        visitor.setVisitor_number(visitorNumber);
        visitor.setVisitor_user_id(userId);
        visitor.setVisitor_createtime(createTime);
        visitor.setVisitor_time(comeTime1);
        visitorService.insert(visitor);
        Visitor visitor1=new Visitor();
        visitor1.setVisitor_user_id(userId);
        ArrayList<Visitor> visitorList=new ArrayList<Visitor>();
        visitorList=visitorService.selectByUserId(visitor1);
        request.getSession().setAttribute("wxvisitorList",visitorList);
        return "true";
    }
    /***
     * 将小程序页面上添加访客的信息传到后台并保存到数据库
     */
    @RequestMapping(value = "/Wxtext",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String Wxtest(HttpServletRequest request){
        User user=new User();
        Host host=new Host();
        String openId=(String) request.getSession().getAttribute("openId");
        user.setUser_open_id(openId);
       User user1=userService.selectByToken(user);
       int host_id=user1.getUser_host_id();
       host.setHost_id(host_id);
      Host  host1=hostservice.selectById(host);
        String host_housenumber=host1.getHost_housenumber()+"";
        String host_building=host1.getHost_building()+"";
        String host_unit=host1.getHost_unit()+"";
        ArrayList<String> HostList=new ArrayList<String>();
        HostList.add(host_housenumber);
        HostList.add(host_building);
        HostList.add(host_unit);
//        request.getSession().setAttribute("HostList",HostList);
        return JSONArray.toJSONString(HostList);
    }
    /***
     * 将访客信息传到前端
     */
    @RequestMapping(value = "/Wxvisitors",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String Wxvisitors(HttpServletRequest request){
        ArrayList<Visitor> visiList=new ArrayList<Visitor>();
        visiList=(ArrayList)request.getSession().getAttribute("wxvisitorList");

        if (visiList!=null&&visiList.size()>0){
            Collections.reverse(visiList);
            ArrayList<Visitor> visitors=new ArrayList<>();
            if (visiList.size()>=3){
                for(int i=0;i<3;i++) {
                    visitors.add(visiList.get(i));
                }
            }else {
                for(int i=0;i<visiList.size();i++) {
                    visitors.add(visiList.get(i));
                }
            }


            return JSONArray.toJSONString(visitors);
        }
        return null;
    }
    /***
     * 将访客信息传到前端
     */
    @RequestMapping(value = "/Wxtry",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String Wxtry(HttpServletRequest request) {
        User user=new User();
        String openId=(String) request.getSession().getAttribute("openId");
        user.setUser_open_id(openId);
        User user1=userService.selectByToken(user);
        int userId=user1.getUser_id();
        Visitor visitor=new Visitor();
        visitor.setVisitor_user_id(userId);
        ArrayList<Visitor> visitorList1=new ArrayList<Visitor>();
        visitorList1=visitorService.selectByUserId(visitor);
        Collections.reverse(visitorList1);
        ArrayList<Visitor> visitors=new ArrayList<>();
        if (visitorList1.size()>=3){
            for(int i=0;i<3;i++) {
                visitors.add(visitorList1.get(i));
            }
        }else {
            for(int i=0;i<visitorList1.size();i++) {
                visitors.add(visitorList1.get(i));
            }
        }

        return JSONArray.toJSONString(visitors);
    }
}
